﻿/*
 * author:ydl
 * function: 添加 xml文件session
 * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcctrueWMS.Foundation.Data;
using System.Xml.Linq;
using System.IO;

namespace AcctrueWMS.Foundation.Web.Session
{
    class SessionManager : ISessionManager
    {
        public void LoadFromFile()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 更具配置的xml配置格式添加 相应的xml
        /// </summary>
        /// <param name="path">xml 文件路径</param>
        public void LoadFromFile(string path)
        {
            try
            {
                if (File.Exists(path))
                {
                    XDocument session = XDocument.Load(path);
                    IEnumerable<XElement> xNode = session.Root.Elements();
                    foreach (XElement xe in xNode)
                    {
                        string ifadd = xe.Attribute("ifadd") == null ? string.Empty : xe.Attribute("ifadd").Value;
                        string sessionKey = xe.Attribute("key") == null ? string.Empty : xe.Attribute("key").Value;
                        string str_SessionValue = xe.Attribute("value") == null ? string.Empty : xe.Attribute("value").Value;
                        Object oSessionValue = CommandExprFactory.Eval(str_SessionValue, Runtime.Context);
                        if (ifadd == "true")
                        {
                            System.Web.HttpContext.Current.Session.Add(sessionKey, oSessionValue);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
